package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    DishService dishService;
    @PostMapping
    public R<String> saveWithFlavor(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("保存成功");
    }
    @GetMapping("page")
    public R<Page<DishDto>> pageDish(Integer page,Integer pageSize,String name){
        Page<DishDto> page1=dishService.pageDish(page,pageSize,name);
        return R.success(page1);
    }
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,Long[] ids){
        dishService.update().set("status",status).in("id",ids).update();
        return R.success("修改成功");
    }
    @GetMapping("{id}")
    public R<DishDto> selectWithFlavor(@PathVariable Long id){
        DishDto dishDto=dishService.selectWithFlavor(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> updateDishWithFlavor(@RequestBody DishDto dishDto){
        dishService.updateDishWithFlavor(dishDto);
        return R.success("修改成功");
    }
    @GetMapping("list")
    public R<List<DishDto>> list(Long categoryId,String name,Integer status){
        /*List<Dish> list = dishService.lambdaQuery()
                .eq(categoryId != null, Dish::getCategoryId, categoryId)
                .eq(StringUtils.isNotBlank(name), Dish::getName, name)
                .eq(Dish::getStatus, 1)
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime)
                .list();*/
        List<DishDto> list=dishService.listWithFlavor(categoryId,name,status);
        return R.success(list);
    }

}
